package seismosurfer.app;

import java.util.List;

import com.bbn.openmap.Environment;
import com.bbn.openmap.PropertyHandler;
import com.bbn.openmap.app.OpenMap;
import com.bbn.openmap.util.Debug;
import com.bbn.openmap.util.PropUtils;
import com.bbn.openmap.gui.OpenMapFrame;
import javax.swing.JFrame;
import seismosurfer.util.ClientRegistry;

/**
 * <code>SeismoSurfer</code> extends <code>OpenMap</code> by adding an
 * overloaded version of <code>create</code> to make it work with the
 * Seismo-Surfer (web) application. <code>OpenMap</code> is designed for a
 * standalone (desktop) application. With this (small) extension it can be used
 * by an applet.
 */

public class SeismoSurfer extends OpenMap {

    /**
     * A PropertyHandler object, which is the organizer of properties, looking
     * for settings on how to configure OpenMap components.
     */
    static private PropertyHandler propertyHandler = null;

    /**
     * Calls OpenMap constructor.
     * 
     * @see OpenMap
     */
    public SeismoSurfer() {
        super();
    }

    /**
     * Calls OpenMap constructor.
     * 
     * @param propertyHandler
     *            an object that handles properties and settings for OpenMap
     *            components
     * @see com.bbn.openmap.PropertyHandler
     * @see com.bbn.openmap.app.OpenMap
     */
    public SeismoSurfer(PropertyHandler propertyHandler) {
        super(propertyHandler);
    }

    /**
     * Create and return a SeismoSurfer object that uses a standard
     * PropertyHandler to configure itself. The SeismoSurfer object has a
     * MapHandler that can be used to gain access to all the components.
     * <p>
     * Also reads Debug parameters and makes some adjustments to make it work
     * with Seismo-Surfer.
     * 
     * @param mapPath
     *            file path to the map (political boundaries) shape file
     * @return SeismoSurfer
     * @see com.bbn.openmap.MapHandler
     * @see com.bbn.openmap.util.Debug 
     */
    public static SeismoSurfer createSeismo(String mapPath) {

        propertyHandler = new PropertyHandler(ClientRegistry
                .getOpenmapProperties());

        String debug = Environment.get("openmap.Debug");

        List keywords = PropUtils.parseSpacedMarkers(debug);

        for (int i = 0; i < keywords.size(); i++) {
            String keyword = (String) keywords.get(i);

            if (keyword.equalsIgnoreCase(Debug.debugAllToken)) {
                Debug.debugAll = true;
            }
            Debug.put(keyword);
            System.out.println(keyword);
        }

        addMapProperties(mapPath);
        return new SeismoSurfer(propertyHandler);
    }

    /**
     * A method called to set the WindowListener behavior on an OpenMapFrame
     * used for the OpenMap application. This method disposes the main frame of
     * the applet.
     */
    public void setWindowListenerOnFrame(OpenMapFrame omf) {
        omf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        ClientRegistry.getHelpBroker().enableHelpKey(omf.getRootPane(), "d0e4",
                null);
    }

    /**
     * Adds properties related to maps to the PropertyHandler. This way the
     * application will read the map related files from the local system.
     * 
     * @param path
     *            local path to map related files without the file extension
     * @see com.bbn.openmap.PropertyHandler
     */
    protected static void addMapProperties(String path) {

        propertyHandler.addProperty("overviewLayer.shapeFile", path + ".shp");
        propertyHandler
                .addProperty("overviewLayer.spatialIndex", path + ".ssx");

        propertyHandler.addProperty("shapePolitical.shapeFile", path + ".shp");
        propertyHandler.addProperty("shapePolitical.spatialIndex", path
                + ".ssx");

    }

}
